package faa.users;

import java.awt.event.*;
import java.util.*;
import faa.*;
import faa.objects.*;
import agent.Agent;

public class User extends Agent {
	protected String username, password;
	protected boolean supervisor=false, clearanceDelivery=false, groundControl=false, localControl=false,
		helicopterControl=false, trafficManager=false;
	private boolean dataChanged=false;
	
	private List<ActionListener> observers = Collections.synchronizedList(new LinkedList<ActionListener>());
	private List<Action> actions;
	private List<Action> finishedActions = Collections.synchronizedList(new LinkedList<Action>());
	
	// Other component connections
	RiskManager riskManager;
	
	private static int actionID = 1;
	
	// Constructors
	public User(String un, String pw) {
		username = un;
		password = pw;
		trafficManager=true;
	}
	
	// Scheduler
	@Override
	protected boolean pickAndExecuteAnAction() {
		if(dataChanged==true) {
			this.notifyObservers();
			return true;
		}
		if(!finishedActions.isEmpty()) {
			clearFinishedAction();
			return true;
		}
		return false;
	}
	
	// Messages
	protected void msgDoThese(List<Action> actionList) {
		actions = actionList;
		dataChanged=true;
		stateChanged();
	}
	protected void msgActionDone(Action act) {
		finishedActions.add(act);
		stateChanged();
	}
	
	// Actions
	protected void notifyObservers() {
		ActionEvent e = new ActionEvent(this, actionID, "Notifying Observers of Datachange");
		actionID++;
		for(ActionListener name : observers) {
			name.actionPerformed(e);
		}
		dataChanged=false;
	}
	protected void clearFinishedAction() {
		Action latest = finishedActions.get(0);
		riskManager.msgActionDone(latest);
		finishedActions.remove(latest);
	}
	
	// Public call-able function  
	public void addDataListener(ActionListener ae) {
		observers.add(ae);
	}
	public void removeDataListener(ActionListener ae) {
		observers.remove(ae);
	}

	// Setters and Getters
	public boolean isSupervisor() {
		return supervisor;
	}

	public void setSupervisor(boolean supervisor) {
		this.supervisor = supervisor;
	}

	public boolean isClearanceDelivery() {
		return clearanceDelivery;
	}

	public void setClearanceDelivery(boolean clearanceDelivery) {
		this.clearanceDelivery = clearanceDelivery;
	}

	public boolean isGroundControl() {
		return groundControl;
	}

	public void setGroundControl(boolean groundControl) {
		this.groundControl = groundControl;
	}

	public boolean isLocalControl() {
		return localControl;
	}

	public void setLocalControl(boolean localControl) {
		this.localControl = localControl;
	}

	public boolean isHelicopterControl() {
		return helicopterControl;
	}

	public void setHelicopterControl(boolean helicopterControl) {
		this.helicopterControl = helicopterControl;
	}

	public boolean isTrafficManager() {
		return trafficManager;
	}

	public void setTrafficManager(boolean trafficManager) {
		this.trafficManager = trafficManager;
	}

	public String getUsername() {
		return username;
	}

	public List<Action> getActions() {
		return actions;
	}

	public void setRiskManager(RiskManager riskManager) {
		this.riskManager = riskManager;
	}
}
